package facade;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import userInterface.GraphicComponents;
import controller.EditorController;

public class Server extends GraphicComponents implements Runnable,CaretListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Server() throws NullPointerException, IOException {
		super();
		
	}

	private static final int PORT = 7002;
    static ObjectInputStream in = null;
    static ObjectOutputStream out = null;
    static Socket socket = null;
    String string=null;
    static boolean  Joinflag=false;
    static boolean Editingflag=false;
    static boolean Updateflag = false;
	public void run() {
		        ServerSocket serverSocket = null;
		        EditorController.flag=false;
		        try {
		            serverSocket = new ServerSocket(PORT);
		        } catch (IOException ex) {
		            System.out.println("Error occured while creating the server socket");
		            return;
		        }
		        
		        try {
		            socket = serverSocket.accept();
		            //Now we have established the a connection with the client
			        System.out.println("Connection created, client IP: " + socket.getInetAddress());
			        if(in==null){in= new ObjectInputStream(socket.getInputStream());}
					if(out==null){out=new ObjectOutputStream(socket.getOutputStream());}
					SerializedMessage message = (SerializedMessage) in.readObject();
			         Joinflag=message.getJoinflag();
		             EditorController.flag=Joinflag;
		            //flag1=false;
		            do{
			        	try {
							carryon1();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						catch (ClassNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}while(Editingflag);
			   
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally{
					try{
						EditorController.flag=false;
						in.close();
						out.close();
						socket.close();
					}
					catch(IOException ioException){
						ioException.printStackTrace();
					}
				}		       
		    }
	public static void PrintToClient(JTextArea text) throws IOException{
		if(in==null){in= new ObjectInputStream(socket.getInputStream());}
		if(out==null){out=new ObjectOutputStream(socket.getOutputStream());}
		out.writeObject(new SerializedMessage(text,true,true));
		out.flush();		
	}
	
	@Override
	public void caretUpdate(CaretEvent arg0) {		
	}

	public static void carryon(JTextArea x){
		try {
               if (in == null) {
                   in = new ObjectInputStream(socket.getInputStream());
               }
               if (out == null) {
                   out = new ObjectOutputStream(socket.getOutputStream());
               }
               out.writeObject(new SerializedMessage(x,true,true));
               out.flush();
           } catch (Exception ex) {
               System.out.println("Error: " + ex);
           }	
   }
	public static void carryon1() throws IOException, ClassNotFoundException{
		 if (in == null) {
             in = new ObjectInputStream(socket.getInputStream());
         }
         if (out == null) {
             out = new ObjectOutputStream(socket.getOutputStream());
         }
         SerializedMessage message = (SerializedMessage) in.readObject();
         text.setText(message.getMessage());
         Editingflag=message.getEditingflag();
	}
}
